package com.newdjk.newdoctor.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.newdjk.newdoctor.MainActivity;
import com.newdjk.newdoctor.MyApplication;
import com.newdjk.newdoctor.R;
import com.newdjk.newdoctor.dialog.SelectedIDcardDialog;
import com.newdjk.newdoctor.dialog.SelectedPictureDialog;
import com.newdjk.newdoctor.entity.AppLicationEntity;
import com.newdjk.newdoctor.entity.QuickMultipleEntity;
import com.newdjk.newdoctor.listener.TabclickListener;
import com.newdjk.newdoctor.ui.InviteDoctorActivity;
import com.newdjk.newdoctor.ui.LoginActivity;
import com.newdjk.newdoctor.ui.MyPatientActivity;
import com.newdjk.newdoctor.ui.ShareShopActivity;
import com.newdjk.newdoctor.ui.WebViewActivity;
import com.newdjk.newdoctor.utils.AppLicationUtils;
import com.newdjk.newdoctor.utils.AppUtils;
import com.newdjk.newdoctor.utils.Contants;
import com.newdjk.newdoctor.utils.LogUtils;
import com.newdjk.newdoctor.utils.ToastUtil;
import com.newdjk.newdoctor.view.ConfiguView;
import com.newdjk.newdoctor.view.MedicineView;
import com.newdjk.newdoctor.view.OperateView;
import com.newdjk.newdoctor.view.TabClickListener;
import com.newdjk.okhttp.entity.MedicationCompanyEntity;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class MultipleItemQuickAdapter extends BaseMultiItemQuickAdapter<QuickMultipleEntity, BaseViewHolder> implements TabClickListener {


    private static final String TAG = "MultipleItemQuickAdapter";
    private SelectedIDcardDialog mSelectedPictureDialog;
    private Activity mActivity;
    private TabclickListener listener;

    public MultipleItemQuickAdapter(List<QuickMultipleEntity> data, Activity activity, TabclickListener listener) {
        super(data);
        // 绑定 layout 对应的 type
        addItemType(QuickMultipleEntity.MEDICAL, R.layout.recyleview_item_medical);
        addItemType(QuickMultipleEntity.OPERATE, R.layout.recyleview_item_operate);
        addItemType(QuickMultipleEntity.CONFIGURE, R.layout.recyleview_item_configure);
        this.mActivity = activity;
        this.listener = listener;
    }

    @Override
    protected void convert(@NotNull BaseViewHolder baseViewHolder, QuickMultipleEntity quickMultipleEntity) {

        LogUtils.d(TAG, "" + baseViewHolder.getItemViewType());
        switch (baseViewHolder.getItemViewType()) {
            case QuickMultipleEntity.MEDICAL:
                MedicineView medicinView = baseViewHolder.getView(R.id.medicin_view);
                medicinView.setTabClick(this);
                medicinView.setData(AppLicationUtils.getMedical());
                break;
            case QuickMultipleEntity.OPERATE:
                OperateView operateView = baseViewHolder.getView(R.id.Operate_View);
                operateView.setTabClick(this);
                operateView.setData(AppLicationUtils.getOperate());
                break;
            case QuickMultipleEntity.CONFIGURE:
                ConfiguView configuView = baseViewHolder.getView(R.id.Configu_View);
                configuView.setTabClick(this);
                configuView.setData(AppLicationUtils.getConfigua());
                break;
        }

    }

    @SuppressLint("LongLogTag")
    @Override
    public void onSessionClick(AppLicationEntity session) {
        Log.d(TAG, "常用" + session);
        String type = session.getAppDesc();  //不需要登录，就能进去的
        if (type.equals("优选推荐") ||
                type.equals("药品集采")
                || type.equals("特色服务推荐") ||
                type.equals("VIP预约推荐") ||
                type.equals("肺癌早筛")) {

            gotoWebview(type);
        } else {
            if (!MyApplication.isLogin) {
                Intent intent = new Intent(getContext(), LoginActivity.class);
                intent.putExtra(Contants.toLogin, Contants.From_mainActivity);
                getContext().startActivity(intent);
                return;
            }
            // int roletype = t.getData().getRoleType();
             if (!type.equals("处方审核")&&!type.equals("购药回访")){
                 if (MyApplication.medicationCompanyEntity == null) {
                     ToastUtil.setToast("请等待数据加载完成");
                     return;
                 }
             }

            //   int roletype =MyApplication.DoctorInfoEntity.getRoleType();
            //  1诊所，2医生，3诊所并且是医生,4，药房  0啥都不是  诊所无申请处方
            //(认证状态。0未认证；1认证中；2已认证；3认证失败)

            if (!type.equals("处方审核")) { //用户管理未认证也能进去
                if (MyApplication.medicationCompanyEntity==null){
                    MyApplication.medicationCompanyEntity=new MedicationCompanyEntity();
                    MyApplication.medicationCompanyEntity.setCheckStatus(MyApplication.DoctorInfoEntity.getDrStatus());
                }
                if (MyApplication.medicationCompanyEntity.getCheckStatus() != 2) {
                    AppUtils.checkAuthenStatus(MyApplication.medicationCompanyEntity.getCheckStatus(), getContext());
                    return;
                }
            }
            gotoWebview(type);
        }

    }

    private void gotoWebview(String type) {
        listener.onHomeFunctionClickListener(type);

    }


    public void reFreshTab(int i, int red, String tabtitle) {
        if (i == 0) {
            List<QuickMultipleEntity> data = getData();
            Log.d("Multiple", data.toString());
            MedicineView medicineView = (MedicineView) getViewByPosition(0, R.id.medicin_view);
            medicineView.setRedTitle(tabtitle, red);

        }


    }
}
